#pragma once

struct M2Header
{
	char Magic[4];
	char Version[4];
	ui32 lenName;
	ui32 ofsName;
	ui32 globalFlags;
	ui32 nGlobalSequences;
	ui32 ofsGlobalSequences;
	ui32 nAnimations;
	ui32 ofsAnimations;
	ui32 nAnimationLookup;
	ui32 ofsAnimationLookup;
	ui32 nBones;
	ui32 ofsBones;
	ui32 nKeyBoneLookup;
	ui32 ofsKeyBoneLookup;
	ui32 nVertices;
	ui32 ofsVertices;
	ui32 nViews;
	ui32 nColors;
	ui32 ofsColors;
	ui32 nTextures;
	ui32 ofsTextures;
	ui32 nTransparencies;
	ui32 ofsTransparencies;
	ui32 nUVAnimations;
	ui32 ofsUVAnimations;
	ui32 nTexReplace;
	ui32 ofsTexReplace;
	ui32 nRenderFlags;
	ui32 ofsRenderFlags;
	ui32 nBoneLookupTables;
	ui32 ofsBoneLookupTables;
	ui32 nTexLookups;
	ui32 ofsTexLookups;
	ui32 nTexUnits;
	ui32 ofsTexUnits;
	ui32 nTransLookups;
	ui32 ofsTransLookups;
	ui32 nUVAnimLookups;
	ui32 ofsUVAnimLookups;
	float VertexMin[3];
	float VertexMax[3];
	float VertexRadius;
	float BoundingMin[3];
	float BoundingMax[3];
	float BoundingRadius;
};

struct M2Texture
{
	ui32 type;
	ui32 flags;
	ui32 lenName;
	ui32 ofsName;
};

struct M2Vertex
{
	float x, y, z;
	char boneWeight[4];
	char boneIndex[4];
	float nx, ny, nz;
	float u, v;
	float unk[2];
};

struct SKINView
{
	ui32 ID, nIndices, ofsIndices, nTriangles, ofsTriangles;
	ui32 nProperties, ofsProperties, nSubMeshes, ofsSubMeshes;
	ui32 nTexUnits, ofsTexUnits, nBones;
};

struct SKINSubMesh
{
	ui32 ID;
	ui16 startVertx, nVertices, startTriangle, nTriangles;
	ui16 nBones, startBone, unk1, unk2;
	float MinX, MinY, MinZ;
	float MaxX, MaxY, MaxZ;
	float Radius;
};

struct SKINTexUnit
{
	ui16 flags, shading, SubMesh1, SubMesh2, ColorIndex;
	ui16 RenderFlags, TexUnitNumber, Mode, Texture, TexUnit2;
	ui16 Transparency, TextureAnim;
};

struct MdxVertex
{
	float X, Y, Z;
	float NX, NY, NZ;
	float U, V;
};

struct M2RenderPass
{
	std::vector<MdxVertex> Vertices;
	std::string Texture;
	ui32 startIndex;
	ui32 nIndices;
};

class M2Info
{
private:
	std::vector<std::string> mTextureNames;
	M2Header mHeader;
	std::vector<M2Texture> mTextures;
	std::vector<M2Vertex> mVertices;
	std::vector<MdxVertex> mModelVertices;
	std::string mModelName;
	std::string mFileDirectory;
	CMpqFile mFile;
	std::vector<M2RenderPass> mPasses;
	ui16* mIndices;
	ui32 nIndices;

	void LoadTextures();
	void ParseVertices();
	void LoadSkins();
public:
	M2Info(const std::string& modName);

	ui32 getNumTextures() const { return mTextureNames.size(); }
	const std::string& getTexture(ui32 index) { return mTextureNames[index]; }
	ui32 getNumPasses() const { return mPasses.size(); }
	const M2RenderPass& getPass(ui32 index) const { return mPasses[index]; }
	ui16* getIndices() { return mIndices; }
	ui32 getNumIndices() { return nIndices; }
	MdxVertex* getVertices() { return &mModelVertices[0]; }
	ui32 getNumVertices() { return mModelVertices.size(); }
};